<?php

/**
 * 
 * @author phuongtn1@fpt.net
 */
class Fpt_Business_Statictis_Adapter_Redis extends Fpt_Business_Statictis_Adapter_Abstract {

    /**
     * @var system_model Singleton instance
     */
    protected static $_instance;

    /**
     * List popular article: article_top_l:{categoryid}:article_type(hot|view|comment) = {article_id=>(hot_date|number_view|number_comment)} 
     */

    const LIST_TOP_NEWS = "list_top_news";

    /**
     * Avoid directly create instance
     */
    protected function __construct() {
        
    }

    /**
     * Retrieve singleton instance
     *
     * @return system_model
     */
    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Reset the singleton instance
     *
     * @return void
     */
    public static function resetInstance() {
        self::$_instance = null;
    }

    /**
     * Return key for type & arg in layout only
     * @param string $type
     * @param string $args
     * @return string
     * @throws Core_Exception 
     */
    public function getKeyByType($type, $args) {
        try {
            $keyCaching = '';
            switch ($type) {
                case self::LIST_TOP_NEWS:
                    $keyCaching = Fpt_Caching_Redis::makeRedisKey(self::LIST_TOP_NEWS, $args);
                    throw new Core_Exception('wrong key', -1);
                    break;
            }
            return $keyCaching;
        } catch (Exception $ex) {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
    }

    /**
     * Get top view article
     * @param type $intSiteID
     * @param type $intTotal
     * @return array List ID of article
     */
    public function getTopViewArticle($intSiteID = SITE_ID, $intTotal = 5, $intArticleType = 1) {
        try {
            $arrResult = array();

            // Get instance redis
            $redisInstance = Fpt_Global::getRedisInstance(0, $intSiteID);

            // Make redis key
            $keyCache = Fpt_Global::makeCacheKey(self::LIST_TOP_NEWS, array($intSiteID, 'view'));

            // Get value to cache
            $arrResult = $redisInstance->zRange($keyCache, 0, $intTotal);
        } catch (Exception $ex) {
            // Log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }

    /**
     * Get top comment article
     * @param type $intSiteID
     * @param type $intTotal 
     * @return array List ID of article
     * @author  : nhanva@fpt.net
     */
    public function getTopCommentArticle($intSiteID = SITE_ID, $intTotal = 5) {

        try {
            $arrResult = array();

            // Get instance redis
            $redisInstance = Fpt_Global::getRedisInstance(0, $intSiteID);

            // Make redis key
            $keyCache = Fpt_Global::makeCacheKey(self::LIST_TOP_NEWS, array($intSiteID, 'comment'));

            // Get value to cache
            $arrResult = $redisInstance->zRange($keyCache, 0, $intTotal);
        } catch (Exception $ex) {
            // Log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }

}